package com.mf.morefun;

import android.os.StrictMode;
import android.support.multidex.MultiDexApplication;
import android.widget.PopupWindow;

import com.apkfuns.logutils.LogLevel;
import com.apkfuns.logutils.LogUtils;
import com.mf.morefun.bean.LoginBean;
import com.mf.morefun.bean.TestBean;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.zhcx.zhcxlibrary.utils.CrashHandler;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class MoreFunApplication extends MultiDexApplication{
    public static DbManager.DaoConfig daoConfig;
    public static LoginBean loginBean;
    public static PopupWindow mNetPop, mIdeaPop;
    public static boolean isOut = false;
    public static List<TestBean> mTestList = new ArrayList<>();//测试题目集合

    @Override
    public void onCreate() {
        super.onCreate();
        //可以不计较安装目录的
        StrictMode.VmPolicy.Builder strBuilder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(strBuilder.build());
        strBuilder.detectFileUriExposure();
        //xutils初始化
        x.Ext.init(this);
//        x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.
        // 全局异常错误捕获对象初始化
        CrashHandler.getInstance().init(getApplicationContext());
        //数据库初始化
        daoConfig = new DbManager.DaoConfig()
                .setDbName("morefun.db")
                .setDbVersion(1)
                .setDbOpenListener(new DbManager.DbOpenListener() {
                    @Override
                    public void onDbOpened(DbManager db) {
                        // 开启WAL, 对写入加速提升巨大
                        db.getDatabase().enableWriteAheadLogging();
                    }
                })
                .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                    @Override
                    public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                        try {
                            if (newVersion < 2) {
                                db.dropDb();//如果设置了监听，那就直接删除了数据库，之前保存的数据就没有了
                            }
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                    }
                });
        //日志打印初始化
        LogUtils.getLogConfig().configAllowLog(false)//是否开启或关闭日志
                .configTagPrefix("WisdomTransit")
                .configFormatTag("%d{HH:mm:ss:SSS} %t %c{-5}")
                .configShowBorders(false)//是否显示Log边框
                .configLevel(LogLevel.TYPE_VERBOSE);
        /**
         * 友盟
         * 初始化common库
         * 参数1:上下文，不能为空
         * 参数2:设备类型，UMConfigure.DEVICE_TYPE_PHONE为手机、UMConfigure.DEVICE_TYPE_BOX为盒子，默认为手机
         * 参数3:Push推送业务的secret
         */
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, null);
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
        //废弃
//        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
        /**
         * 设置组件化的Log开关
         * 参数: boolean 默认为false，如需查看LOG设置为true
         */
        UMConfigure.setLogEnabled(false);
        /**
         * 设置日志加密
         * 参数：boolean 默认为false（不加密）
         */
        UMConfigure.setEncryptEnabled(true);
        //SDK通过Thread.UncaughtExceptionHandler 捕获程序崩溃日志
        MobclickAgent.setCatchUncaughtExceptions(true);
        initShare();
    }

    private void initShare() {
        PlatformConfig.setWeixin("wx84f6c60e3f190823", "8ff3018e7c669e9a5cdb51e27b8db01b");
        UMShareAPI.get(this);
    }
}
